package com.bookstore.servlet.home;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bookstore.model.Book;
import com.bookstore.model.Category;
import com.bookstore.model.Product;
import com.bookstore.service.ProductService;
import com.bookstore.service.ServiceFactory;

@WebServlet("/main.do")
public class MainServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("method");
		Map<String, List> datas = new HashMap<String, List>();
		if (method != null) {
			switch (method) {
			case "1":
				productDetail(request, response);
				break;
			}
		} else {
			initCategory(datas);
			initRecommend(datas);
			initHotBooks(datas);
			initNewBooks(datas);

			request.setAttribute("datas", datas);
			request.getRequestDispatcher("/WEB-INF/view/home/main.jsp").forward(request, response);
		}
	}

	private void initNewBooks(Map<String, List> datas) {
		List<Book> books = new ArrayList<>();
		List<Product> products = null;
		try {
			products = ServiceFactory.getProductService().findByNew(4);
			for (Product product : products) {
				books.add((Book) product);
			}
			datas.put("newBooks", books);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void initHotBooks(Map<String, List> datas) {
		List<Book> books = new ArrayList<>();
		List<Product> products = null;
		try {
			products = ServiceFactory.getProductService().findByHod(4);
			for (Product product : products) {
				books.add((Book) product);
			}
			datas.put("hotBooks", books);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void initRecommend(Map<String, List> datas) {
		List<Book> list = new ArrayList<>();
		ProductService productService = ServiceFactory.getProductService();
		try {
			list.add((Book) productService.findById(new Random().nextInt(20)));
			list.add((Book) productService.findById(new Random().nextInt(20)));
			datas.put("books", list);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void productDetail(HttpServletRequest request, HttpServletResponse response) {
		Book book = null;
		String id = request.getParameter("id");
		try {
			if (id != null) {
				book = (Book) ServiceFactory.getProductService().findById(new Integer(id));
			}
			request.setAttribute("book", book);
			request.getRequestDispatcher("/WEB-INF/view/home/product.jsp").forward(request, response);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void initCategory(Map<String, List> datas) {
		List<Category> categories = null;
		try {
			List<Category> list = ServiceFactory.getCategoryService().findAll();
			categories = findByParentId(list, 1);
			for (Category category : categories) {
				category.setSubCats(findByParentId(list, category.getId()));
			}
			datas.put("category", categories);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private List<Category> findByParentId(List<Category> list, int id) {
		List<Category> subCategories = new ArrayList<>();
		for (Category category : list) {
			if (category.getParentId() == id) {
				subCategories.add(category);
			}
		}
		return subCategories;
	}

}
